#include "DlgLogin.h"
#include "PublicData.h"
#include <math.h>
#include <wx/statbox.h>
#include <wx/sizer.h>
#include <wx/stattext.h>
#include <wx/msgdlg.h>
#include <wx/log.h>
//(*InternalHeaders(DlgType)
#include <wx/intl.h>
#include <wx/string.h>
//*)

//(*IdInit(DlgType)
//*)

const wxString WINDOWS_TEXT = _T("用户登录");
//控件ID
const long CDlgLogin::IDC_BTN_LOGIN= wxNewId();
const long CDlgLogin::IDC_BTN_EXIT= wxNewId();
const long CDlgLogin::IDC_EDIT_NAME= wxNewId();
const long CDlgLogin::IDC_EDIT_PWD= wxNewId();

BEGIN_EVENT_TABLE(CDlgLogin,wxDialog)
    //(*EventTable(DlgType)
    //*)
END_EVENT_TABLE()

CDlgLogin::CDlgLogin(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(DlgType)
    Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER|wxCLOSE_BOX|wxMAXIMIZE_BOX|wxMINIMIZE_BOX, _T("id"));
    //*)
    SetTitle(WINDOWS_TEXT);
    //SetWindowStyle(wxDEFAULT_DIALOG_STYLE);
    CreateCtrlAndEvent();
}

CDlgLogin::~CDlgLogin()
{
    //(*Destroy(DlgType)
    //*)
}

void CDlgLogin::CreateCtrlAndEvent()
{
    /////////创建控件并布局
    topSizer_=new wxBoxSizer(wxVERTICAL);//顶层窗口布局,从上到下包含每个了窗口布局
    wxPanel *panel = new wxPanel(this, wxID_ANY);
    wxBoxSizer *firSize=new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer *secSize=new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer *bottomSize=new wxBoxSizer(wxHORIZONTAL);
    panel->SetSizer(topSizer_);//绑定对话框和布局控件
    topSizer_->Add(firSize,0,wxEXPAND|wxALL,0);
    topSizer_->Add(secSize,0,wxEXPAND|wxALL,0);
    topSizer_->Add(bottomSize,0,wxEXPAND|wxALL,0);

    btn_Login_=new wxButton(panel,IDC_BTN_LOGIN,_T("登录"));
    btn_Login_->SetDefault();
    btn_Exit_=new wxButton(panel,IDC_BTN_EXIT,_T("退出"));
    text_name_=new wxTextCtrl(panel,IDC_EDIT_NAME,wxEmptyString);
    text_name_->SetFocus();
    text_name_->SetToolTip(_T("初始化用户名与密码均为admin"));
    text_pwd_=new wxTextCtrl(panel,IDC_EDIT_PWD,wxEmptyString,wxDefaultPosition,wxDefaultSize,wxTE_PASSWORD);
    text_pwd_->SetToolTip(_T("初始化用户名与密码均为admin"));

    firSize->Add(new wxStaticText(panel,wxID_ANY,_T("用户名")),0, wxFIXED_MINSIZE|wxALL|wxALIGN_RIGHT,5);
    firSize->Add(text_name_,1, wxEXPAND|wxALL|wxALIGN_LEFT,5);
    secSize->Add(new wxStaticText(panel,wxID_ANY,_T("密码")),0,wxFIXED_MINSIZE|wxALL|wxALIGN_RIGHT,5);
    secSize->Add(text_pwd_,1,wxEXPAND|wxALL|wxALIGN_LEFT,5);
    bottomSize->Add(btn_Login_,1,wxFIXED_MINSIZE|wxALL|wxALIGN_RIGHT,5);
    bottomSize->Add(btn_Exit_,1,wxFIXED_MINSIZE|wxALL|wxALIGN_LEFT,5);

    panel->SetSizer(topSizer_);//绑定对话框和布局控件
    topSizer_->Fit(this);//调用对话框大小
    topSizer_->SetSizeHints(this);//设置对话框最小大小
    ///////创建事件
    Connect(IDC_BTN_LOGIN,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&CDlgLogin::OnBnClickedBtnLogin);
    Connect(IDC_BTN_EXIT,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&CDlgLogin::OnBnClickedBtnExit);
    Connect(wxEVT_INIT_DIALOG,(wxObjectEventFunction)&CDlgLogin::OnInitDialog);

    wxLogDebug(_T("CreateCtrl"));
}
void CDlgLogin::OnInitDialog(wxInitDialogEvent& event)
{
    Centre();
}
void CDlgLogin::OnBnClickedBtnLogin(wxCommandEvent& event)
{
    wxBusyCursor waticursor;
    wxString csName=wxDynamicCast(FindWindowById(IDC_EDIT_NAME),wxTextCtrl)->GetValue();
    wxString csPWD=wxDynamicCast(FindWindowById(IDC_EDIT_PWD),wxTextCtrl)->GetValue();
    wxString sql=_T("select userid,username,userPermit from user where userID=@pa1 and userPWD=@pa2");
    sqlite3_stmt *stmt;
    if(sqlite3_prepare_v2(g_db,ToDefaultConv(sql),LenDefaultConv(sql),&stmt,NULL)==SQLITE_OK) {
        int index1 = sqlite3_bind_parameter_index(stmt, "@pa1");
        if(sqlite3_bind_text(stmt, index1, ToDefaultConv(csName), LenDefaultConv(csName), SQLITE_STATIC)!=SQLITE_OK) {
            wxLogDebug(_T("sqlite3_bind_text on m_keyvalue_.first Error on. %s"), sqlite3_errmsg(g_db));
        }
        index1 = sqlite3_bind_parameter_index(stmt, "@pa2");
        if(sqlite3_bind_text(stmt, index1, ToDefaultConv(csPWD), LenDefaultConv(csPWD), SQLITE_STATIC)!=SQLITE_OK) {
            wxLogDebug(_T("sqlite3_bind_text on ntimeslot Error on. %s"), sqlite3_errmsg(g_db));
        }
        if(sqlite3_step(stmt)==SQLITE_ROW) {
            if(sqlite3_column_text(stmt,0)==NULL) {
                wxMessageBox(_T("用户名或密码有误"),_T("登录失败"),wxOK);
            } else {
                g_userid=wxString((char*)sqlite3_column_text(stmt,0),DefaultConv);
                g_username=wxString((char*)sqlite3_column_text(stmt,1),DefaultConv);
                g_userpermit=sqlite3_column_int(stmt,2);
                EndDialog(wxID_OK);
            }
        } else {
            wxMessageBox(_T("用户名或密码有误"),_T("登录失败"),wxOK);
        }
    } else {
        wxLogDebug(_T("Error on sqlite3_prepare_v2: %s"), sqlite3_errmsg(g_db));
    }
    if (sqlite3_finalize(stmt) != SQLITE_OK) {
        wxLogDebug(_T("Error on sqlite3_finalize: %s"), sqlite3_errmsg(g_db));
    }
    wxLogDebug(_T("OnBnClickedBtnLogin"));
}
void CDlgLogin::OnBnClickedBtnExit(wxCommandEvent& event)
{
    Close();
    wxLogDebug(_T("OnBnClickedBtnExit"));
}
